package io.github.cchenxi.crpc.simple.example.provider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import io.github.cchenxi.crpc.core.annotation.RpcProvider;
import io.github.cchenxi.crpc.simple.example.api.User;
import io.github.cchenxi.crpc.simple.example.api.UserService;

/**
 * 用户API实现
 * Date: 2024-03-06
 *
 * @author chenxi
 */
@Component
@RpcProvider
public class UserServiceImpl implements UserService {
    @Autowired
    Environment env;

    @Override
    public User findById(int id) {
        return new User(id, "crpc-example-" + env.getProperty("server.port") + System.currentTimeMillis());
    }

    @Override
    public User findById(int id, String name) {
        return new User(id, name + "-"  + env.getProperty("server.port") + System.currentTimeMillis());
    }

    @Override
    public String getName(int id) {
        return "crpc-example-" + env.getProperty("server.port") + System.currentTimeMillis();
    }

    @Override
    public long getId(long id) {
        return id;
    }

    @Override
    public long getId(float id) {
        return 1L;
    }

    @Override
    public long getId(User user) {
        return user.getId().longValue();
    }

    @Override
    public int[] getIds(int id) {
        return new int[]{1, 2, 3};
    }

    @Override
    public float[] getIds(float id) {
        return new float[]{1.1f, 2.2f, 3.3f};
    }

    @Override
    public long[] getIdsFromIds(int[] ids) {
        return new long[]{4, 5, 6};
    }
}
